﻿#include "uimodelyktableview.h"

UiModelYkTableView::UiModelYkTableView(QWidget *parent)
    : UiFilterTableView{parent}
{
    connect(m_addBtn, &QPushButton::clicked, this, &UiModelYkTableView::addYk);
}

void UiModelYkTableView::reload(const QString &modelname, const QString &user)
{
    this->clearData();
    if(modelname.isEmpty()) {
        m_modelName.clear();
        m_user.clear();
        return;
    }

    m_user = user;
    m_modelName = modelname;


    QList<QSharedPointer<IModel>> yks;
    if(!CWebRequestApi::ins().getModelYkList(yks, modelname))
        return;

    this->appendData(yks);
}

void UiModelYkTableView::addYk()
{
    if(m_modelName.isEmpty()) {
        ElaMessageBar::error(ElaMessageBarType::Top, "警告", "请先选择操作模型!",2000);
        return;
    }

    QSharedPointer<CRtModelYkInfo> info(new CRtModelYkInfo);

    int max_sn = 0;
    QStringList groups;
    QStringList names;
    for(int row = 0; row < m_model->rowCount(); ++row) {
        CRtModelYkInfo* Ykptr = static_cast<CRtModelYkInfo*>(m_model->getDataByIndex(row).data()) ;
        if(!Ykptr)
            continue;

        if(!groups.contains(Ykptr->grouping))
            groups.append(Ykptr->grouping);

        if(!names.contains(Ykptr->name))
            names.append(Ykptr->name);

        if(Ykptr->sn >= max_sn)
            max_sn = Ykptr->sn;
    }

    info->model_name = m_modelName;
    info->user = m_user;
    info->sn = max_sn+1;
    info->name = "";

    UiModelSigBaseEdit editw(info, groups,UiModelSigBaseEdit::ECreate);

    CYxValueEditPlugin* editbox = new CYxValueEditPlugin;
    QString curValue = info->getFieldValueByFielddName(ykColFieldName(YK_ENUM::EValues)).toString();
    if(!editbox->parseData(curValue)) {
        ElaMessageBar::error(ElaMessageBarType::Top, "系统错误", "遥控值编辑插件加载失败!",2000);
        return;
    }

    QVBoxLayout* mainlayout = new QVBoxLayout;
    QVBoxLayout* layout = new QVBoxLayout;

    QGroupBox* box = new QGroupBox;
    box->setTitle("遥控值编辑");
    layout->addWidget(editbox);
    box->setLayout(layout);

    QWidget* wtmp = new QWidget;

    int fontSize = 16;
    int lineHeidht = 32;

    ElaText tmp_enwrite("写使能:");
    tmp_enwrite.setMinimumHeight(lineHeidht);
    tmp_enwrite.setTextPointSize(fontSize);
    ElaToggleSwitch btn_enwrite;
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_enwrite,1);
    hlayout->addWidget(&btn_enwrite,3);

    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addWidget(box);
    vlayout->addLayout(hlayout);
    wtmp->setLayout(vlayout);

    mainlayout->addWidget(&editw);
    mainlayout->addWidget(wtmp);

    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->resize(500, 400);
    dialog->setConten(mainlayout);
    while(dialog->exec() == UiCustomDialog::Accepted) {
        editw.save();

        if(!editbox->checkData()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "值映射重复或者值描述为空!",2000);
            continue;
        }

        if(names.contains(info->name)){
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信号名字重复,请重新输入!",2000);
            continue;
        }

        if(info->name.isEmpty() || info->descr.isEmpty() || info->grouping.isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信息不能为空,请检查!",2000);
            continue;
        }

        if(editbox->vcount() > 0) {
            QString buf = editbox->readData();
            QJsonDocument doc = QJsonDocument::fromJson(buf.toUtf8());

            QJsonObject jsonvalues = doc.object();
            info->valuesFromJson(jsonvalues["valuesmap"].toArray());
        }

        info->en_write = btn_enwrite.getIsToggled();

        if(!CWebRequestApi::ins().addYk(info)) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "添加遥控失败",2000);
            break;
        }

        this->appendData(info);
        ElaMessageBar::success(ElaMessageBarType::Top, "完成", "添加遥控成功",1500);
        break;
    }
    dialog->deleteLater();
}
